package com.hurl.waguji.biz.download;

import java.util.List;

import org.testng.log4testng.Logger;

import com.hurl.library.spider.UrlPattern;
import com.hurl.library.util.StringUtils;
import com.hurl.waguji.base.Formatter;
import com.hurl.waguji.base.dataprocess.ProcessParams;
import com.hurl.waguji.persistence.DmGpBO;
import com.hurl.waguji.persistence.JcsjGdThsBO;
import com.hurl.waguji.persistence.JcsjLtgdThsBO;

class ProfileGdThsProcessor extends BaseDownloadForUpdateProcessor {

	public ProfileGdThsProcessor(ProcessParams params,
			DownloadIdentifier identifier) {
		super(params, identifier);
	}
	private static final Logger log = Logger.getLogger(ProfileGdThsProcessor.class);
	
	@Override
	public void convertHTML2BO(String url, String html, BOSaver collector) {

		if(StringUtils.isBlank(html)){
			return;
		}
		String gpdm = context.getEnvAsString("gpdm");
		
		String lt = StringUtils.substringBetween(html, "id=\"lt\">十大流通股东</div>", "gdyj_5");
		if(StringUtils.isNotBlank(lt)){
			List<String> rqlist = StringUtils.substringsBetween(lt, "class=\"sdgd\" href=\"javascript:void(0);\">", "</a></div>");
			if(rqlist==null || rqlist.isEmpty()){
				return;
			}
			List<String> tablelist = StringUtils.substringsBetween(lt, "<table id=\"ltTable\">", "</table>");
			if(tablelist==null || tablelist.isEmpty()){
				return;
			}
			if(rqlist.size()!=tablelist.size()){
				log.error("日期和数据不匹配");
				return;
			}
			String table = null;
			List<String> trlist = null;
			String tr = null;
			List<String> tdlist = null;
			JcsjLtgdThsBO bo = null;
			String jgmc = null;
			String gflx = null;
			String cgs = null;
			String zltgbl = null;
			String jc = null;

			context.getDao().queryByHQL("delete from "+JcsjLtgdThsBO.class.getSimpleName()+" where gpDm='"+gpdm+"'").executeUpdate();
			for(int i=0;i<tablelist.size();i++){
				table = tablelist.get(i);
				trlist = StringUtils.substringsBetween(table, "<tr>", "</tr>");
				for(int j=1;j<trlist.size();j++){
					tr = trlist.get(j);
					tdlist = StringUtils.substringsBetween(tr, "<td", "</td>");
					if(tdlist==null || tdlist.size()!=5){
						log.error("流通股东数据格式错误");
					}
					jgmc = StringUtils.substringBetween(tdlist.get(0), "<div class=\"gdyj_12\">", "</div>");
					gflx = StringUtils.substringBetween(tdlist.get(1), "<span>", "</span>");
					cgs = StringUtils.substringBetween(tdlist.get(2), "<span>", "</span>");
					zltgbl = StringUtils.substringBetween(tdlist.get(3),"<span>", "</span>");
					if(zltgbl!=null && zltgbl.contains("%")){
						zltgbl = zltgbl.replace("%", "");
					}
					jc = StringUtils.substringBetween(tdlist.get(4), "number_right\">", null);
					if(jc!=null){
						jc = jc.trim();
						if(jc.contains("<span>")){
							jc = StringUtils.substringBetween(jc, "<span>", "</span>");
						}
					}
					bo = new JcsjLtgdThsBO();
					bo.setGpDm(gpdm);
					bo.setRq(Formatter.toDate(rqlist.get(i)));
					bo.setJgmc(Formatter.toString(jgmc));
					bo.setGflx(Formatter.toString(gflx));
					Double cgsD = Formatter.toDouble(cgs);
					if(cgsD!=null){
						cgsD*=10000;
					}
					bo.setCgs(cgsD);
					Double jcD = Formatter.toDouble(jc);
					if(jcD!=null){
						jcD*=10000;
					}
					bo.setJc(jcD);
					Double zltgblD = Formatter.toDouble(zltgbl);
					if(zltgblD!=null){
						zltgblD/=100;
					}
					bo.setZltgbbl(zltgblD);
					bo.setLastupdate(currentTime);
					collector.addBO(bo);
				}
			}
		}
		
		String gd = StringUtils.substringBetween(html, "id=\"sd\">十大股东</div>", "股东研究");
		if(StringUtils.isNotBlank(gd)){
			List<String> rqlist = StringUtils.substringsBetween(gd, "<a href=\"javascript:void(0);\">", "</a></div>");
			if(rqlist==null || rqlist.isEmpty()){
				return;
			}
			List<String> tablelist = StringUtils.substringsBetween(gd, "<table id=\"sdTable\">", "</table>");
			if(tablelist==null || tablelist.isEmpty()){
				return;
			}
			if(rqlist.size()!=tablelist.size()){
				log.error("日期和数据不匹配");
			}
			String table = null;
			List<String> trlist = null;
			String tr = null;
			List<String> tdlist = null;
			JcsjGdThsBO bo = null;
			String jgmc = null;
			String gflx = null;
			String cgs = null;
			String zzgbbl = null;
			String jc = null;

			context.getDao().queryByHQL("delete from "+JcsjGdThsBO.class.getSimpleName()+" where gpDm='"+gpdm+"'").executeUpdate();
			for(int i=0;i<tablelist.size();i++){
				table = tablelist.get(i);
				trlist = StringUtils.substringsBetween(table, "<tr>", "</tr>");
				for(int j=1;j<trlist.size();j++){
					tr = trlist.get(j);
					tdlist = StringUtils.substringsBetween(tr, "<td", "</td>");
					if(tdlist==null || tdlist.size()!=5){
						log.error("流通股东数据格式错误");
					}
					bo = new JcsjGdThsBO();
					jgmc = StringUtils.substringBetween(tdlist.get(0), "<div class=\"gdyj_12\">", "</div>");
					gflx = StringUtils.substringBetween(tdlist.get(1), "<span>", "</span>");
					cgs = StringUtils.substringBetween(tdlist.get(2), "<span>", "</span>");
					zzgbbl = StringUtils.substringBetween(tdlist.get(3),"<span>", "</span>");
					if(zzgbbl!=null && zzgbbl.contains("%")){
						zzgbbl = zzgbbl.replace("%", "");
					}
					jc = StringUtils.substringBetween(tdlist.get(4), "number_right\">", null);
					if(jc!=null){
						jc = jc.trim();
						if(jc.contains("<span>")){
							jc = StringUtils.substringBetween(jc, "<span>", "</span>");
						}
					}
					bo.setGpDm(gpdm);
					bo.setRq(Formatter.toDate(rqlist.get(i)));
					bo.setJgmc(Formatter.toString(jgmc));
					bo.setGflx(Formatter.toString(gflx));
					Double cgsD = Formatter.toDouble(cgs);
					if(cgsD!=null){
						cgsD*=10000;
					}
					bo.setCgs(cgsD);
					Double jcD = Formatter.toDouble(jc);
					if(jcD!=null){
						jcD*=10000;
					}
					bo.setJc(jcD);

					Double zzgbblD = Formatter.toDouble(zzgbbl);
					if(zzgbblD!=null){
						zzgbblD/=100;
					}
					bo.setZzgbbl(zzgbblD);
					bo.setLastupdate(currentTime);
					
					collector.addBO(bo);
				}
			}
		}
	
	}

	@Override
	public UrlPattern getUrlPattern() {
		return new StockIteratorUrlPattern(context) {
			
			@Override
			public String bo2url(DmGpBO bo) {
				return "http://basic.10jqka.com.cn/"+bo.getGpDmJ()+"/gdyj.html";
			}
		};
	}

}
